<?php


namespace App\Services\Result;

use App\Interf\Resultable;
use Monolog\Logger;

use ParamConfig;
use App\Models\LogHelper;

class PostpartumWeightRetentionTestingResult extends BaseResult implements Resultable
{
    private $answers;
    private $summaryObj;
    private $tipsObj;

    public function __construct($answersStr = null)
    {

        $this->answers = $this->convertAnswer($answersStr);
        $this->summaryObj = $this->configAttrObj(1,'测评结果');
        $this->tipsObj = $this->configAttrObj(2, '生活技巧');


    }


    public function result()
    {

        $attrs = [];

        array_push($attrs, $this->getSummaryAttr());
        array_push($attrs, $this->getTipsAttr());
        $data['attrs'] = $attrs;
        $data['chatparam'] = $this->getChatparam();
        //图类型常规图
        $data['chatType'] = [];
        return $data;
    }


    protected function getSummaryAttr()
    {
        $note = '';
        $extra = '';

        $answer18= $this->getDefaultAnswerObjByIndex(18);
        $answer23= $this->getDefaultAnswerObjByIndex(23);
        $answer7= $this->getDefaultAnswerObjByIndex(7);
        $answer8= $this->getDefaultAnswerObjByIndex(8);
        $answer9= $this->getDefaultAnswerObjByIndex(9);
        $answer12= $this->getDefaultAnswerObjByIndex(12);

        $answer6= $this->getDefaultAnswerObjByIndex(6);
        $answer10= $this->getDefaultAnswerObjByIndex(10);
        $answer11= $this->getDefaultAnswerObjByIndex(11);

        $temp = ParamConfig::getValue('postpartum.weight.retention.testing.summary.one.one');
        $note .= str_replace('$18',$answer18['answer'],$temp);


        if ($answer23['weight'] == 1) {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.summary.two.one');
            $note .= $temp;
        } elseif ($answer23['weight'] == 0) {
            if($answer7['weight'] == 1 || $answer8['weight'] == 1 || $answer9['weight'] == 1 ||  $answer12['weight'] == 1)
            {
                $temp = ParamConfig::getValue('postpartum.weight.retention.testing.summary.three.one');
                $note .= $temp;
                if($answer7['weight'] == 1 || $answer8['weight'] == 1)
                {
                    $temp = ParamConfig::getValue('postpartum.weight.retention.testing.summary.three.two');
                    $note .= $temp;
                }
                if($answer9['weight'] == 1)
                {
                    $temp = ParamConfig::getValue('postpartum.weight.retention.testing.summary.three.three');
                    $note .= $temp;
                }
                if($answer12['weight'] == 1)
                {
                    $temp = ParamConfig::getValue('postpartum.weight.retention.testing.summary.three.four');
                    $note .= $temp;
                }
            }elseif (($answer6['weight'] ==1 || $answer10['weight'] ==1 || $answer11['weight'] ==1) &&  $answer7['weight'] == 0 && $answer8['weight'] == 0 && $answer9['weight'] == 0 && $answer12['weight'] == 0) {
                $temp = ParamConfig::getValue('postpartum.weight.retention.testing.summary.four.one');
                $note .= $temp;
                if($answer6['weight'] ==1 && $answer7['weight'] ==0 && $answer8['weight'] ==0 && $answer9['weight'] ==0 && $answer12['weight'] ==0)
                {
                    $temp = ParamConfig::getValue('postpartum.weight.retention.testing.summary.four.two');
                    $note .= $temp;
                }
                if($answer10['weight'] ==1 && $answer7['weight'] ==0 && $answer8['weight'] ==0 && $answer9['weight'] ==0 && $answer12['weight'] ==0)
                {
                    $temp = ParamConfig::getValue('postpartum.weight.retention.testing.summary.four.three');
                    $note .= $temp;
                }
                if($answer11['weight'] ==1 && $answer7['weight'] ==0 && $answer8['weight'] ==0 && $answer9['weight'] ==0 && $answer12['weight'] ==0)
                {
                    $temp = ParamConfig::getValue('postpartum.weight.retention.testing.summary.four.four');
                    $note .= $temp;
                }



            } else {
                $temp = ParamConfig::getValue('postpartum.weight.retention.testing.summary.five.one');
                $note .= $temp;
            }

        }

        $obj = $this->summaryObj;
        $obj = $this->compactAttrObj($obj['attr_id'], $obj['title'], $note, $extra);

        LogHelper::log($obj);
        return $obj;
    }


    protected function getTipsAttr()
    {
        $note = '';
        $extra = '';

        $answer16= $this->getDefaultAnswerObjByIndex(16);
        $answer14= $this->getDefaultAnswerObjByIndex(14);
        $answer15= $this->getDefaultAnswerObjByIndex(15);
        $answer10= $this->getDefaultAnswerObjByIndex(10);
        $answer11= $this->getDefaultAnswerObjByIndex(11);
        $answer13= $this->getDefaultAnswerObjByIndex(13);

        $answer1= $this->getDefaultAnswerObjByIndex(1);
        $answer17= $this->getDefaultAnswerObjByIndex(17);

        if($answer16['weight'] == 1)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.one.one');
            $note .= $temp;
        }elseif ($answer16['weight'] == 0)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.one.two');
            $note .= $temp;
        }

        if($answer14['weight'] == 1 && $answer15['weight'] == 1)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.two.one');
            $note .= $temp;
        }elseif ($answer14['weight'] == 0 && $answer15['weight'] == 0)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.two.two');
            $note .= $temp;
        }elseif ($answer14['weight'] == 1 && $answer15['weight'] == 0)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.two.three');
            $note .= $temp;
        }
        elseif ($answer14['weight'] == 0 && $answer15['weight'] == 1)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.two.four');
            $note .= $temp;
        }

        if($answer10['weight'] == 0)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.three.one');
            $note .= $temp;
        }

        if($answer11['weight'] == 0)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.four.one');
            $note .= $temp;
        }

        if($answer13['weight'] == 0)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.five.one');
            $note .= $temp;
        }elseif ($answer13['weight'] == 1)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.five.two');
            $note .= $temp;
        }

        if($answer1['weight'] == 0)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.six.one');
            $note .= $temp;
        }elseif ($answer1['weight'] == 1)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.six.two');
            $note .= $temp;
        }


        if($answer17['weight'] == 0)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.seven.one');
            $note .= $temp;
        }elseif ($answer17['weight'] == 1)
        {
            $temp = ParamConfig::getValue('postpartum.weight.retention.testing.tips.seven.two');
            $note .= $temp;
        }


        $obj = $this->tipsObj;
        $obj = $this->compactAttrObj($obj['attr_id'], $obj['title'], $note, $extra);

        LogHelper::log($obj);
        return $obj;
    }

    protected function getChatparam()
    {
        $data = [];

        return $data;
    }

    protected function compactAttrObj($id, $title, $note, $extra = '')
    {
        return [
            'attr_id' => $id,
            'title' => $title,
            'note' => $note,
            'extra' => $extra
        ];
    }

    protected function getDefaultAnswerObjByIndex($index)
    {
        $obj = [];
        foreach ($this->answers as $answer) {
            if ($index == $answer['index']) {
                $obj = $answer;
                break;
            }
        }
        return $obj;
    }

    /**将answer转换为数组
     * @param $answerStr
     * @return array
     */
    protected function convertAnswer($answerStr)
    {
        LogHelper::log('FINAL ANSWER STR============>');
        LogHelper::log($answerStr);
        $answers = [];
        $answerArr = explode('##', $answerStr);
        if (count($answerArr) > 0) {
            foreach ($answerArr as $answer) {
                $singleArr = explode(':', $answer);
                if (count($singleArr) > 0) {
                    $obj['index'] = $singleArr[0];
                    $obj['weight'] = $singleArr[1];
                    $obj['answer'] = $singleArr[2];
                    array_push($answers, $obj);
                }
            }
        }
        return $answers;
    }

}